#pragma once

#include "Triangle.h"
#include "SphereTree.h"

class CMesh
{
public:
	CMesh(const D3DXCOLOR& color);
	virtual ~CMesh(void);

	void Destroy();

	void LoadFromFile(const wchar_t* szFileName);
	void BuildSphereTree();

	HRESULT InitD3DBuffer(LPDIRECT3DDEVICE9 pDevice9);
	void Render(LPDIRECT3DDEVICE9 pDevice9, BOOL bSphere, int iTreeLevel = 0);
	void OnLostDevice();
	void OnResetDevice();

	int GetNumVertices() const { return m_Vertices.size(); }
	int GetNumTriangles() const { return m_Primitives.size(); }

	int GetNodeCount(int iLevel) const;

	void SetTranslation(const D3DXVECTOR3& vTranslation) { m_vTranslation = vTranslation; }

	void FinalizeMesh();

	const CSphereTree* GetSphereTree() const { return m_pSphereTree; }

	void GetBounds(DATA_TYPE* pDomainMinOut, DATA_TYPE* pDomainMaxOut);

private:

	void AddVertices(DATA_TYPE x, DATA_TYPE y, DATA_TYPE z);
	void AddPrimitive(int index1, int index2, int index3);

	std::vector<VectorR> m_Vertices;
	std::vector<CTriangle> m_Primitives;

	LPDIRECT3DVERTEXBUFFER9 m_pVB; // Buffer to hold vertices
	LPDIRECT3DINDEXBUFFER9 m_pIB; // Buffer to hold vertices

	VectorR m_vOBBCenter;
	VectorR m_vEigenVectors[3];

	CSphereTree* m_pSphereTree;
	D3DXCOLOR m_color;

	D3DXVECTOR3 m_vTranslation;
};

